home *** CD-ROM | disk | FTP | other *** search
/ Mac Format 1994 October / Macformat17.cdr / Shareware City / Developers / shutdown-fx-201-c / sfx ƒ / sfx control app ƒ / sfx code ƒ / sfx install.c < prev    next >
Text File  |  1994-07-11  |  5KB  |  178 lines

  1. /**********************************************************************\
  2.  
  3. File:        sfx install.c
  4.  
  5. Purpose:    This module handles installing shutdown procs and installing
  6.             and removing fades from the system heap.
  7.  
  8. This program is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 2 of the License, or
  11. (at your option) any later version.
  12.  
  13. This program is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. GNU General Public License for more details.
  17.  
  18. You should have received a copy of the GNU General Public License
  19. along with this program in a file named "GNU General Public License".
  20. If not, write to the Free Software Foundation, 675 Mass Ave,
  21. Cambridge, MA 02139, USA.
  22.  
  23. \**********************************************************************/
  24.  
  25. #include "Shutdown.h"
  26. #include "sfx install.h"
  27. #include "sfx gestalt.h"
  28. #include "file interface.h"
  29. #include "util.h"
  30.  
  31. enum ErrorTypes InstallTheShutdownProc(short index, Str255 theName, Boolean onShutdown,
  32.     Boolean onRestart, Boolean isEarly, Boolean installFade, Boolean installProcs)
  33. /* assume: there are no shutdown procs currently installed */
  34. /* assume: sfx Gestalt function is properly installed */
  35. {
  36.     OSErr            theResError;
  37.     FSSpec            fs;
  38.     short            oldRefNum, newRefNum;
  39.     Boolean            alreadyOpen;
  40.     Handle            theProcHandle;
  41.     ProcPtr            theFade, theShutdownProc, theRestartProc;
  42.     unsigned long    theSize;
  43.     unsigned long    dummy;
  44.     enum ErrorTypes    resultCode;
  45.     
  46.     if (installFade)
  47.     {
  48.         MyMakeFSSpec(gModuleVRefNum, gModuleDirID, theName, &fs);
  49.         theResError=OpenTheResFile(&fs, &oldRefNum, &newRefNum, &alreadyOpen);
  50.         if (theResError!=noErr)
  51.             return kCantOpenModule;
  52.         
  53.         theProcHandle=Get1Resource('PROC', 0);
  54.         if (((theResError=ResError())!=noErr) || (theProcHandle==0L))
  55.             return kModuleDamaged;
  56.         
  57.         if (*theProcHandle==0L)
  58.             LoadResource(theProcHandle);
  59.         if (*theProcHandle==0L)
  60.             return kModuleDamaged;
  61.         
  62.         theProcHandle=StripAddress(theProcHandle);
  63.         *theProcHandle=StripAddress(*theProcHandle);
  64.         
  65.         HLock(theProcHandle);
  66.         theFade=NewPtrSys(theSize=GetHandleSize(theProcHandle));
  67.         if (theFade==0L)
  68.         {
  69.             ReleaseResource(theProcHandle);
  70.             return kNoMemoryInSystemHeap;
  71.         }
  72.         
  73.         theFade=StripAddress(theFade);
  74.         
  75.         Mymemcpy(theFade, *theProcHandle, theSize);
  76.         ReleaseResource(theProcHandle);
  77.         theProcHandle=0L;
  78.         CloseTheResFile(oldRefNum, newRefNum, alreadyOpen);
  79.     }
  80.     else theFade=0L;
  81.     
  82.     if (installProcs)
  83.     {
  84.         theProcHandle=Get1Resource('PROC', 10);    /* get shutdown proc from sfx application */
  85.         if (((theResError=ResError())!=noErr) || (theProcHandle==0L))
  86.             return kCantGetProc10Resource;
  87.     
  88.         if (*theProcHandle==0L)
  89.             LoadResource(theProcHandle);
  90.         if (*theProcHandle==0L)
  91.         {
  92.             DisposePtr(theFade);
  93.             return kCantGetProc10Resource;
  94.         }
  95.         
  96.         theProcHandle=StripAddress(theProcHandle);
  97.         *theProcHandle=StripAddress(*theProcHandle);
  98.         
  99.         theShutdownProc=NewPtrSys(theSize=GetHandleSize(theProcHandle));
  100.         theRestartProc=NewPtrSys(theSize);
  101.         
  102.         theShutdownProc=StripAddress(theShutdownProc);
  103.         theRestartProc=StripAddress(theRestartProc);
  104.         
  105.         Mymemcpy(theShutdownProc, *theProcHandle, theSize);
  106.         Mymemcpy(theRestartProc, *theProcHandle, theSize);
  107.         ReleaseResource(theProcHandle);
  108.         theProcHandle=0L;
  109.     }
  110.     else
  111.     {
  112.         resultCode=GetGestaltProcPtrs(&dummy, &theShutdownProc, &theRestartProc);
  113.         if (resultCode!=allsWell)
  114.             return resultCode;
  115.     }
  116.     
  117.     *(ProcPtr*)((long)theShutdownProc+6)=
  118.         *(ProcPtr*)((long)theRestartProc+6)=theFade;            /* store address of fade */
  119.     *(short*)((long)theShutdownProc+10)=onShutdown ? 1 : 0;        /* store on shutdown flag */
  120.     *(short*)((long)theRestartProc+10)=onRestart ? 1 : 0;        /* store on restart flag */
  121.     if (installProcs)
  122.     {
  123.         ShutDwnInstall(theShutdownProc, isEarly ? sdOnPowerOff+sdOnDrivers : sdOnPowerOff);
  124.         ShutDwnInstall(theRestartProc, isEarly ? sdOnRestart+sdOnDrivers: sdOnRestart);
  125.     }
  126.     gModuleIndex=index;
  127.     resultCode=SetGestaltProcPtrs(theFade, theShutdownProc, theRestartProc);
  128.     
  129.     return resultCode;
  130. }
  131.  
  132. enum ErrorTypes RemoveTheFade(void)
  133. {
  134.     ProcPtr            theFade, theShutdownProc, theRestartProc;
  135.     enum ErrorTypes    resultCode;
  136.     
  137.     resultCode=GetGestaltProcPtrs(&theFade, &theShutdownProc, &theRestartProc);
  138.     if (resultCode!=allsWell)
  139.         return resultCode;
  140.  
  141.     resultCode=SetGestaltProcPtrs(0L, theShutdownProc, theRestartProc);
  142.     if (resultCode!=allsWell)
  143.         return resultCode;
  144.     
  145.     if (theFade!=0L)
  146.         DisposePtr(theFade);
  147.     
  148.     if (theShutdownProc!=0L)
  149.         *(short*)((long)theShutdownProc+10)=0;
  150.     
  151.     if (theRestartProc!=0L)
  152.         *(short*)((long)theRestartProc+10)=0;
  153.     
  154.     return allsWell;
  155. }
  156.  
  157. OSErr OpenTheResFile(FSSpec* fs, short* oldRefNum, short* newRefNum, short* alreadyOpen)
  158. {
  159.     unsigned long    oldTopMapHndl;
  160.     OSErr            theResError;
  161.     
  162.     *oldRefNum=CurResFile();
  163.     oldTopMapHndl=(unsigned long)TopMapHndl;
  164.     *newRefNum=HOpenResFile(fs->vRefNum, fs->parID, fs->name, fsRdPerm);
  165.     theResError=ResError();
  166.     *alreadyOpen=(oldTopMapHndl==(unsigned long)TopMapHndl);
  167.     return theResError;
  168. }
  169.  
  170. void CloseTheResFile(short oldRefNum, short newRefNum, Boolean alreadyOpen)
  171. {
  172.     if (!alreadyOpen)
  173.     {
  174.         CloseResFile(newRefNum);
  175.     }
  176.     UseResFile(oldRefNum);
  177. }
  178.